package org.agilelang.generator.webservices.php;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.agilelang.mvc.Generator;
import org.agilelang.mvc.IModel;
import org.agilelang.object.Model;
import org.agilelang.object.Project;
import org.agilelang.object.WebServices;
import org.agilelang.object.attributes.Attribute;
import org.agilelang.object.attributes.Reference;

public class GWebServicesOld extends Generator implements IModel {

    public GWebServicesOld(String path, Project project) {
        this.path = path;
        this.models = project.models;
        this.webclients = project.webclient;
        this.webservices = project.webservices;
        this.setTemplate2("webservices/php");
        this.template = "bootstrap";
    }

    public void process() {
        int t = webservices.size();
        for (int i = 0; i < t; i++) {

            WebServices webservice = webservices.get(i);

            //Model model = models.get(i);
            Model model = this.getModelByName(webservice.model);

            String list_ws_save = "";

            String list_ws_find = "";

            List<Attribute> listattr = model.attributes;
            Collections.sort(listattr, new CustomComparator());
            int ta = listattr.size();
            for (int j = 0; j < ta; j++) {
                Attribute attr = listattr.get(j);
                list_ws_save += "$_GET['" + attr.name + "'], ";
                if (attr.field.equals("true")) {
                    list_ws_find += "\tif ($_GET['f'] == \"find_by_" + attr.name + "\") {\n"
                            + "\t\t$" + this.setSingularize(model.name).toLowerCase() + "->findBy" + this.setSingularize(attr.name) + "($_GET['from'], $_GET['to'], $_GET['" + attr.name + "']);\n"
                            + "\t}\n";
                }
            }
            int tr = model.references.size();
            for (int j = 0; j < tr; j++) {
                Reference reference = model.references.get(j);
                list_ws_find += "\tif ($_GET['f'] == \"get_list_" + reference.model_reference.toLowerCase() + "\") {\n"
                        + "\t\t$" + this.setSingularize(model.name).toLowerCase() + "->getList" + reference.model_reference + "();\n"
                        + "\t}\n";
            }



            String file = readStreamTpl("m_webservices_old.php.tpl");
            file = file.replace("//NAMECLASS//", model.name);
            file = file.replace("//nameclass//", model.name.toLowerCase());
            file = file.replace("//list_ws_save//", list_ws_save.substring(0, list_ws_save.length() - 2));
            file = file.replace("//list_ws_find//", list_ws_find);
            createfile("/webservices/" + model.name.toLowerCase() + "_ws.php", path, file);
        }
    }

    public class CustomComparator implements Comparator<Attribute> {

        @Override
        public int compare(Attribute o1, Attribute o2) {
            return o1.item.compareTo(o2.item);
        }
    }
}
